package com.fh.controller.system.tools;
import com.fh.util.PageData;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import java.util.ArrayList;
import java.util.List;

public class PushToApp {

    // 定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置

    private static String appId = "24fJWPl5BJ9rL6uDcQVeQ7";
    private static String appKey = "dPMBI7hxEr6UcBjaJh5V73";
    private static String masterSecret = "BnsEJ4UqFA77fDRvcQ2HZ1";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";






   /* static String appId = "nSJtifqVSI7HkPrKHlxhD6";
    static String appkey = "WlZGdJlcUB8ds32Y2Thn91";
    static String mastersecret = "5vjiwMEaij5VvYf7VhlGM4";
    static String CID = "3e170b169630706f82baf94c8a2b8923";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";
    static String Alias = "aliastest";
*/

  /*  IGtPush push = new IGtPush(host, appkey, mastersecret);*/

   /* IAliasResult bindSCid = push.bindAlias(appId, Alias, CID);*/

    /*
     * 个推 title 推送标题 text 推送内容 list clientid数组
     */
    public static void PushPrivate(String title, String text, List<String> list,String code) {
        // 显示每个用户的用户状态，false:不显示，true：显示
        System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
        // 推送主类
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        try {
            ListMessage message = new ListMessage();
            // 通知模版：支持TransmissionTemplate、LinkTemplate、NotificationTemplate，此处以NotificationTemplate为例
            // 在通知栏显示一条含图标、标题等的通知，用户点击后激活您的应用
            TransmissionTemplate template = new TransmissionTemplate();
            template.setAppId(appId);
            template.setAppkey(appKey);
            // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
            template.setTransmissionType(2);
            template.setTransmissionContent(code);

            // 设置定时展示时间
            message.setData(template);
            message.setOffline(true); //用户当前不在线时，是否离线存储，可选，默认不存储
            message.setOfflineExpireTime(72 * 3600 * 1000); //离线有效时间，单位为毫秒，可选
            // 接收者
            List<Target> targets = new ArrayList<Target>();
            if (list.size() > 0 && list != null) {
                for(String clientId : list){
                    Target target = new Target();
                    target.setAppId(appId); // 接收者安装的应用的APPID
                    target.setClientId(clientId); // 接收者的ClientID
                    targets.add(target);
                }
            }
            // 推送前通过该接口申请“ContentID”
            String contentId = push.getContentId(message);
            IPushResult ret = push.pushMessageToList(contentId, targets);
            System.out.println(ret.getResponse().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


     /*
     * 公推 title 推送标题 text 推送内容
     */

   /* public static void PushPulic(String title, String text) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle(title);
        template.setText(text);
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        template.setTransmissionType(1);
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);
        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
    }
*/


   /*   public static void main(String[] args) throws Exception {
String id="285fb2fc13254153a58dae5d02dfe897";
        String title="合同待审核";
        String text="带审核的合同（透传的内容是合同的ID，看看这样的数据去传送参数能否是用？{ID:285fb2fc13254153a58dae5d02dfe897}）";
       *//* String code ="{"+"ID"+":"+id+"}";*//*
        JSONObject json =new JSONObject();
        json.put("ID",id);
       String index = json.toString();
        String text1="带审核的合同（透传的内容是合同的ID，看看这样的数据去传送参数能否是用？{ID:285fb2fc13254153a58dae5d02dfe897}）";
        List<String> list =new ArrayList<String>();
        list.add("f12db120c31783d4aa037a88a0293acc");
         *//*      PushPrivate(title,text,list,index);*//*
        PushPrivate(title,text,list,index);
    }*/

    /*
    * 获取用户的cid通过权限
    * */
    public  static List<String> userCID(String role, List<PageData> list) {
        List<String> listcid = new ArrayList<String>();
        if(list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                String[] index = list.get(i).get("AUTH_CODE").toString().split(",");
                for (int j=0 ;j<index.length;j++) {
                    if(index[j].equals(role)){
                        listcid.add(list.get(i).get("CID").toString());
                    }
                }
            }
        }
        return  listcid;
    }

   /* public static void main(String[] args) throws Exception {



    }*/


}